package com.avantis.admin.controller;

import com.avantis.admin.config.shiro.CurrentUser;
import com.avantis.admin.service.SysSerialNumberService;
import com.avantis.common.base.BaseController;
import com.avantis.common.entity.AdminUser;
import com.avantis.common.entity.Customer;
import com.avantis.common.entity.SysSerialNumber;
import com.avantis.common.entity.query.SysSerialNumberQuery;
import com.avantis.common.msg.YResult;
import com.avantis.common.utils.Constants;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: tracywang
 * @Date: 2019/2/16
 * @Descrption:
 */
@RestController
@RequestMapping("/serialNumber")
public class SysSerialNumberController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysSerialNumberController.class);

    @Autowired
    private SysSerialNumberService sysSerialNumberService;

    @PostMapping("/list")
    @ApiOperation(value = "编码规则列表", httpMethod = "POST", notes = "")
    public YResult list() {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("datas",sysSerialNumberService.findSysSerialNumberCriteria());
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "添加编码规则信息", httpMethod = "POST", notes = "")
    public YResult insert(@RequestBody @Validated SysSerialNumber sysSerialNumber) {
        YResult yResult = restProcessor(() -> {
                AdminUser adminUser = CurrentUser.get();
                sysSerialNumber.setCreater(adminUser.getUsername());
                sysSerialNumber.setModifier(adminUser.getUsername());
                sysSerialNumber.setCreatedon(new Date());
                sysSerialNumber.setModifyon(new Date());
                sysSerialNumberService.saveSysSerialNumber(sysSerialNumber);
                return YResult.ok();
                });
        return yResult;
        }

    @GetMapping("/detail/{sysSerialNumberId}")
    @ApiOperation(value = "编码规则信息详情", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "sysSerialNumberId", value = "编码规则ID", dataType = "int"))
    public YResult detail(@PathVariable("sysSerialNumberId") String sysSerialNumberId) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("sysSerialNumber",sysSerialNumberService.findByUuid(sysSerialNumberId));
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @DeleteMapping("/delete/{sysSerialNumberId}")
    @ApiOperation(value = "删除编码规则信息", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "sysSerialNumberId", value = "编码规则ID", dataType = "int"))
    public YResult delete(@PathVariable("sysSerialNumberId") String sysSerialNumberId) {
        YResult yResult = restProcessor(() -> {
            try {
                sysSerialNumberService.deleteSysSerialNumber(sysSerialNumberId);
                return YResult.ok();
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/modify")
    @ApiOperation(value = "修改编码规则信息", httpMethod = "POST", notes = "")
    public YResult modify(@RequestBody @Validated SysSerialNumber sysSerialNumber) {
        YResult yResult = restProcessor(() -> {
            AdminUser adminUser = CurrentUser.get();
            sysSerialNumber.setModifier(adminUser.getUsername());
            sysSerialNumber.setModifyon(new Date());
            sysSerialNumberService.modifySysSerialNumber(sysSerialNumber);
            return YResult.ok();
        });
        return yResult;
    }
}
